/*
 * @Author: wangc
 * @Date: 2021-03-15 21:22:47
 * @Description: 接口请求配置
 */

import qs from 'qs'
import { Toast } from 'vant'
import { throwErr } from './throwErr'
import { getInfo } from '@/utils/auth'
Toast.setDefaultOptions({ duration: 1000 }) // duration延迟时间

var options = {
    // 默认url前缀
    baseURL: process.env.VUE_APP_BASE_URL,
    // 超时
    timeout: 20,
    headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json'
    }
}

export function exportBaseUrl(flag) {
    if("user" === flag) {
        return process.env.VUE_APP_USER_URL;        //88
    } else if("restApi" === flag) {
        return process.env.VUE_APP_YARN_URL;        //87
    } else if("ip" === flag) {
        return process.env.VUE_APP_IP_URL;      //88
    } else if("res" === flag) {
        return process.env.VUE_APP_UPLOAD_URL;      //90
    } else {
        return process.env.VUE_APP_BASE_URL;        //89
    }
}

export function request(config, callBack) {

    // 处理默认请求地址
    options.baseURL = config.isUser ?
        process.env.VUE_APP_USER_URL :
        config.isYarn ?
        process.env.VUE_APP_YARN_URL :
        config.isRes ?
        process.env.VUE_APP_UPLOAD_URL :
        process.env.VUE_APP_BASE_URL


    if (!config.hidden) {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 20000,
            loadingType: 'spinner'
        })
    }

    // 处理请求头
    if (config.data && config.data.captcha) {
        options.headers['j-session-id'] = config.data.captcha
    }

    
    if (config.isFile) {
        options.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    }

    if(config.data.isTokenFlag) {
        options.headers['x-yd-access-token'] = config.data.accessToken;
        options.headers['x-yd-user-id'] = config.data.userId;
        options.headers['x-yd-platform'] = 3;
    } else if (getInfo()) {
        let userInfo = getInfo()
        options.headers['x-yd-access-token'] = userInfo.accessToken
        options.headers['x-yd-user-id'] = userInfo.userId
        options.headers['x-yd-platform'] = 3
    }

    if (config.data && config.isJoin) {
        if (config.isKong) {
            config.url = config.url + (config.data.standard ? '?standard=' + config.data.standard : '')
        } else if (config.isSeries) {
            config.url = config.url + (config.data.name ? '?name=' + config.data.name : '')
        } else {
            if (typeof(config.data) === 'string') {
                config.url = config.url + '/' + config.data
            } else {
                config.url = config.url + '?' + qs.stringify(config.data)
            }
        }
    }

    let data = {}
    if (config.method === 'post' && config.type === 'json') {
        data = {
            body: JSON.stringify(config.data)
        }
        options.headers['Content-Type'] = 'application/json'
    } else {
        if (config.isFlag) {
            if (config.data.flag) {
                config.url = config.url + '/' + config.data.geryClothId + '?flag=' + config.data.flag
            } else {
                config.url = config.url + '/' + config.data.geryClothId
            }
            data = {
                values: {}
            }
        } else if (config.isId) {
            config.url = config.url + '/' + config.data
            data = {
                values: {}
            }
        } else {
            data = {
                values: config.data,
                files: config.data.file || {}
            }
        }
    }

        // 请求
    window.api.ajax({
        url: config.isResUrl ? config.data.resUrl : options.baseURL + config.url,
        method: config.method || 'post',
        dataType: config.type || 'json',
        returnAll: false,
        timeout: options.timeout,
        headers: options.headers || {},
        data: config.isKong ? {} : data
    }, function(res, err) {
        Toast.clear()
        if (res.code === 2000) {
            return callBack(res.data)
        } else {
            if(err.code) {
                return callBack(err);
            }
            if (config.isEmpty) {
                return callBack(res)
            }
            return throwErr(res, config.isAcept)
        }
    })
}